package com.joinpay.controller;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.joinpay.service.GroupService;
import com.joinpay.service.UserService;

@Controller
public class UserController {

	@Resource
	private UserService userService;
	@Resource
	private GroupService groupService;

	@RequestMapping(produces = "text/plain;charset=UTF-8", value = "loginValidate", method = RequestMethod.POST)
	public String loginValidate(HttpServletRequest request) throws Exception {
		Map<String, String> userMap = new HashMap<String, String>();
		try {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			if (username.equals("") || password.equals("")) {
				return "html/index";
			}
			userMap.put("username", username);
			userMap.put("password", password);
			boolean flag = userService.loginValidate(userMap);
			if (flag) {
				request.getSession().setAttribute("username", username);
				return "html/test";
			}
			return "html/index";
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "html/index";
		}
	}

	/**
	 * 插入新用户
	 */
	@RequestMapping(produces = "text/plain;charset=UTF-8", value = "addUser", method = RequestMethod.POST)
	@ResponseBody
	public String addUser(HttpServletRequest request) {
		Map<String, Object> mapSend = new HashMap<String, Object>();
		try {
			String departID = request.getParameter("departID");
			String currentUser = (String) request.getSession().getAttribute("username");
			mapSend.put("currentUser", currentUser);
			mapSend.put("username", request.getParameter("username"));
			mapSend.put("password", request.getParameter("password"));
			mapSend.put("nickname", request.getParameter("nickname"));
			mapSend.put("departID", departID);
			mapSend.put("groupName", groupService.getCompleteGroup(departID));
			mapSend.put("power", request.getParameter("power"));
			int sign = userService.insertUser(mapSend);
			mapSend.put("flag", "success");
			if (sign == 0) {
				mapSend.put("flag", "fail");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			mapSend.put("flag", "fail");
		}
		return JSON.toJSONString(mapSend);
	}

	/**
	 * 更新用户
	 */
	@RequestMapping(produces = "text/plain;charset=UTF-8", value = "updateUser", method = RequestMethod.POST)
	@ResponseBody
	public String updateUser(HttpServletRequest request) {
		Map<String, Object> mapSend = new HashMap<String, Object>();
		try {
			String departID = request.getParameter("departID");
			String currentUser = (String) request.getSession().getAttribute("username");
			mapSend.put("currentUser", currentUser);
			mapSend.put("id", request.getParameter("id"));
			mapSend.put("username", request.getParameter("username"));
			mapSend.put("password", request.getParameter("password"));
			mapSend.put("nickname", request.getParameter("nickname"));
			mapSend.put("departID", departID);
			mapSend.put("groupName", groupService.getCompleteGroup(departID));
			mapSend.put("power", request.getParameter("power"));
			int sign = userService.updateUser(mapSend);
			mapSend.put("flag", "success");
			if (sign == 0) {
				mapSend.put("flag", "fail");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			mapSend.put("flag", "fail");
		}
		return JSON.toJSONString(mapSend);
	}
}
